package com.beiming.libra.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.beiming.libra.common.Assert;
import com.beiming.libra.common.constants.ValidateMessage;
import com.beiming.libra.common.enums.ErrorCode;
import com.beiming.libra.common.enums.PlatformEnum;
import com.beiming.libra.dao.AuthRoleRelationMapper;
import com.beiming.libra.dao.RoleMapper;
import com.beiming.libra.domain.AuthRoleRelation;
import com.beiming.libra.domain.MenuInfo;
import com.beiming.libra.domain.Role;
import com.beiming.libra.domain.dto.requestdto.AuthRoleRelationRequestDTO;
import com.beiming.libra.domain.dto.requestdto.RoleRequestDTO;
import com.beiming.libra.service.AuthRoleRelationService;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

/***
 * 
 * @author weibo
 */
@Service
@Slf4j
public class AuthRoleRelationServiceImpl implements AuthRoleRelationService {
  @Resource
  private AuthRoleRelationMapper authRoleRelationMapper;
  @Resource
  private RoleMapper roleMapper;

  /** 删除角色权限关系 */
  @Override
  public void deleteAuthRoleRelation(String roleId) {
    Example authRoleRelationExample = new Example(AuthRoleRelation.class, false);
    authRoleRelationExample.createCriteria().andEqualTo("roleId", roleId);
    authRoleRelationMapper.deleteByExample(authRoleRelationExample);
  }

  /** 新增角色权限关系 */
  @Override
  @Transactional
  public void insertAuthRoleRelation(AuthRoleRelationRequestDTO authRoleRelationRequestDTO) {
    RoleRequestDTO roleRequestDTO = new RoleRequestDTO();
    roleRequestDTO.setId(authRoleRelationRequestDTO.getRoleId());
    String roleId = authRoleRelationRequestDTO.getRoleId();
    Role role = roleMapper.selectByPrimaryKey(roleId);
    Assert.isNotNull(role, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_EXISTS);
    if (StringUtils.isEmpty(authRoleRelationRequestDTO.getListMenuId())) {
      // 清除所有的权限权限关系
      deleteAuthRoleRelation(roleId);
      return;
    }
    // 新的角色权限id
    String[] menuId = authRoleRelationRequestDTO.getListMenuId().split(",");
    // 查询数据库已经存在的角色和权限对应关系
    Example selectExample = new Example(AuthRoleRelation.class, false);
    selectExample.createCriteria().andEqualTo("roleId", roleId);
    List<AuthRoleRelation> oldList = authRoleRelationMapper.selectByExample(selectExample);
    // 保存需要新增的集合
    List<AuthRoleRelation> addList = new ArrayList<AuthRoleRelation>();
    // 删除需要删除的集合
    List<AuthRoleRelation> deleteList = new ArrayList<AuthRoleRelation>();
    // 新的角色权限关系
    List<AuthRoleRelation> newList = new ArrayList<AuthRoleRelation>();
    for (int i = 0; i < menuId.length; i++) {
      String menuIdStr = menuId[i];
      AuthRoleRelation authRoleRelation = null;
      for (AuthRoleRelation authRole : oldList) {
        if (authRole.getMenuId().equals(menuIdStr)) {
          authRoleRelation = authRole;
          break;
        }
      }
      if (authRoleRelation == null) {
        authRoleRelation = new AuthRoleRelation();
        authRoleRelation.setMenuId(menuIdStr);
        authRoleRelation.setRoleId(roleId);
        authRoleRelation.setCreateUser(authRoleRelationRequestDTO.getCreateUser());
        authRoleRelation.setUpdateUser(authRoleRelationRequestDTO.getUpdateUser());
        addList.add(authRoleRelation);
      }
      newList.add(authRoleRelation);
    }

    for (AuthRoleRelation authRole : oldList) {
      boolean bol = false;
      for (AuthRoleRelation authRoleRelation : newList) {
        if (authRoleRelation.getMenuId().equals(authRole.getMenuId())) {
          bol = true;
          break;
        }
      }
      if (!bol) {
        deleteList.add(authRole);
      }
    }
    // 插入
    if (addList.size() > 0) {
      authRoleRelationMapper.insertList(addList);
    }
    if (deleteList.size() > 0) {
      Example authRoleRelationExample = new Example(AuthRoleRelation.class, false, false);
      List<String> deleIdList = new ArrayList<String>();
      for (AuthRoleRelation authRoleRelation : deleteList) {
        deleIdList.add(authRoleRelation.getId());
      }
      authRoleRelationExample.createCriteria().andIn("id", deleIdList);
      // 删除 不存在的权限角色关系
      authRoleRelationMapper.deleteByExample(authRoleRelationExample);
    }
  }

  /** 获取角色权限关系 */
  @Override
  public List<AuthRoleRelation> getAuthRoleRelation(String roleId) {
    Example examole = new Example(AuthRoleRelation.class, false);
    examole.createCriteria().andEqualTo("roleId", roleId);
    return authRoleRelationMapper.selectByExample(examole);
  }

  @Override
  public List<MenuInfo> getAuthInfoByUserId(String userId, PlatformEnum platformEnum) {
    Assert.isNotNull(userId, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    Assert.isNotNull(platformEnum, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.PLATFORM_NOT_NULL);
    return authRoleRelationMapper.getAuthInfoByUserIdMapper(userId, platformEnum.getCode());
  }
}
